"""
游戏启动器 - 支持原始游戏和AI对战模式
"""
import pygame
import sys
from settings import *
from main import Main
from ai_vs_player_mode import AIVsPlayerMain


class GameLauncher:
    """游戏启动器类"""
    
    def __init__(self):
        """初始化启动器"""
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("球球大战 - 启动器")
        self.clock = pygame.time.Clock()
        self.running = True
        self.font_large = pygame.font.Font(None, 64)
        self.font_medium = pygame.font.Font(None, 48)
        self.font_small = pygame.font.Font(None, 32)
        
        # 菜单选项
        self.menu_options = [
            {"text": "原始游戏", "action": self.start_original_game},
            {"text": "AI对战模式", "action": self.start_ai_vs_player},
            {"text": "退出", "action": self.quit}
        ]
        
        self.selected_option = 0
    
    def run(self):
        """运行启动器"""
        while self.running:
            self.handle_events()
            self.draw()
            self.clock.tick(FPS)
        
        pygame.quit()
        sys.exit()
    
    def handle_events(self):
        """处理事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.selected_option = (self.selected_option - 1) % len(self.menu_options)
                elif event.key == pygame.K_DOWN:
                    self.selected_option = (self.selected_option + 1) % len(self.menu_options)
                elif event.key == pygame.K_RETURN:
                    # 执行选中的选项
                    self.menu_options[self.selected_option]["action"]()
    
    def draw(self):
        """绘制启动器界面"""
        self.screen.fill(BLACK)
        
        # 绘制标题
        title_text = self.font_large.render("球球大战", True, WHITE)
        title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 4))
        self.screen.blit(title_text, title_rect)
        
        # 绘制菜单选项
        for i, option in enumerate(self.menu_options):
            color = GREEN if i == self.selected_option else WHITE
            option_text = self.font_medium.render(option["text"], True, color)
            option_rect = option_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + i * 60))
            self.screen.blit(option_text, option_rect)
        
        # 绘制操作提示
        help_text = self.font_small.render("使用上下箭头键选择，回车键确认", True, WHITE)
        help_rect = help_text.get_rect(center=(WIDTH // 2, HEIGHT - 50))
        self.screen.blit(help_text, help_rect)
        
        pygame.display.flip()
    
    def start_original_game(self):
        """启动原始游戏"""
        print("启动原始游戏...")
        main_game = Main()
        main_game.run()
        self.running = False  # 退出启动器
    
    def start_ai_vs_player(self):
        """启动AI对战模式"""
        print("启动AI对战模式...")
        ai_vs_player = AIVsPlayerMain()
        ai_vs_player.run()
        self.running = False  # 退出启动器
    
    def quit(self):
        """退出游戏"""
        self.running = False


if __name__ == '__main__':
    """程序入口点"""
    launcher = GameLauncher()
    launcher.run()